package com.carassistant.beta.phone;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.carassistant.beta.R;
import com.carassistant.beta.common.dialog.MAlertDialog;
import com.carassistant.beta.db.DBDao;
import com.carassistant.beta.db.DBData;
import com.carassistant.beta.phone.adapter.FavoriteAdapter;

public class PhoneFavActivity extends Activity implements OnItemClickListener,OnItemLongClickListener{
	private ListView favoriteList; 
	private FavoriteAdapter favoriteAdapter = null;
	
	public static final String REQUEIR_NUMBER = "get_number";
	public static final int REQUEIR_NUMBER_CODE = 5; 
	public static final int RESULT_NUMBER_CODE = 6; 
	private int onClickEntryView = 0; // 当前view的ID值
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_favorite);
		
		favoriteList = (ListView) findViewById(R.id.list_view);
		DBDao dbDao = new DBDao(this);
		ArrayList<ContentValues> allFavNumber = dbDao.getAllCustomPhone();
		dbDao.close();
		favoriteAdapter = new FavoriteAdapter(this,allFavNumber);
		favoriteList.setAdapter(favoriteAdapter);
		favoriteList.setOnItemClickListener(this);
		favoriteList.setOnItemLongClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		TextView favoriteName = (TextView) view.findViewById(R.id.favoriteName);
		TextView favoriteNumber = (TextView) view.findViewById(R.id.favoriteNumber);
		if(favoriteName.getText() == null || favoriteName.getText() == ""){
			onClickEntryView = position;
			Intent intent = new Intent(this, PhoneContactActivity.class);
			intent.putExtra(REQUEIR_NUMBER, REQUEIR_NUMBER_CODE);
			startActivityForResult(intent, REQUEIR_NUMBER_CODE); 
		}else{
			startActivity(getDailIntent(favoriteNumber.getText().toString()));
		}
	}
	
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		if (view.getTag() == null) {
			return true;
		} 
		
		onClickEntryView = position;
		getEntryDialog(onClickEntryView);
		return true;
	}
	
	private void getEntryDialog(final int whichButton){
    	final MAlertDialog btDialog = new MAlertDialog(this, MAlertDialog.DIALOG_LIST); 
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item, 
    			getResources().getStringArray(R.array.derect_call_messages));
    	btDialog.setTitle("收藏夹");   
    	btDialog.setAdapter(adapter, new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0) {
					DBDao dbDao = new DBDao(PhoneFavActivity.this);
					dbDao.deleteCustomPhone(whichButton);
					dbDao.close();
					myHandler.sendEmptyMessageDelayed(1, 30);
				} else {
					Intent intent = new Intent(PhoneFavActivity.this, PhoneContactActivity.class);
					intent.putExtra(REQUEIR_NUMBER, REQUEIR_NUMBER_CODE);
					startActivityForResult(intent, REQUEIR_NUMBER_CODE); 
				}
				btDialog.dismiss();
			}
		}); 
    	btDialog.show();
    }
	
	private Intent getDailIntent(String number){
		if (number != null && !"".equals(number)) {
			Uri callUri = Uri.parse("tel:" + number);
			return new Intent(Intent.ACTION_CALL, callUri);
		} 
		return null;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REQUEIR_NUMBER_CODE && data != null) {
			String name = (String) data.getStringExtra(PhoneContactActivity.NAME);
			String number = (String) data.getStringExtra(PhoneContactActivity.NUMBER);
			ContentValues values = new ContentValues();
			values.put(DBData.PHONE_ID, onClickEntryView);
			values.put(DBData.PHONE_NUMBER, number);
			if (name == null) {
				values.put(DBData.PHONE_CONTACT_NAME, number);
			} else {
				values.put(DBData.PHONE_CONTACT_NAME, name);
			} 
			
			DBDao dbDao = new DBDao(PhoneFavActivity.this);
			dbDao.insertCustomPhone(values);
			dbDao.close();
			
			myHandler.sendEmptyMessageDelayed(1, 30);
		}
	}
	
	// 线程
    Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case 1:
				DBDao dbDao = new DBDao(PhoneFavActivity.this);
				ArrayList<ContentValues> allFavNumber = dbDao.getAllCustomPhone();
				dbDao.close();
				
				favoriteAdapter.refresh(allFavNumber);
				break;
			}
		}	
    };
}
